package cn.wolfcode.shop.cloud.web.controller;



import cn.wolfcode.shop.cloud.BusinessException;
import cn.wolfcode.shop.cloud.RedisService;
import cn.wolfcode.shop.cloud.Result;
import cn.wolfcode.shop.cloud.domain.User;
import cn.wolfcode.shop.cloud.msg.SecKillCodeMsg;
import cn.wolfcode.shop.cloud.util.SecKillKeyPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

@Controller
public class PathController {
    @Autowired
    private RedisService redisService;

    @RequestMapping("/getPath")
    @ResponseBody
    public Result getPath(String uuid, String verifyCode,  User user){

        if(user == null){
            return Result.error(SecKillCodeMsg.KILL_OPT);
        }

        String code = redisService.get(SecKillKeyPrefix.SECKILL_VERIFYCODE,uuid,String.class);



        if (verifyCode.equals(code)) {
            String path = UUID.randomUUID().toString().replace("-","");
            redisService.set(SecKillKeyPrefix.SECKILL_PATH,uuid,path);
            return Result.success(path);
        }else {
            throw new BusinessException(SecKillCodeMsg.KILL_OPT);
        }


    }
}
